package cn.itsource.pet.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.DistanceUtil;
import cn.itsource.basic.util.Point;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.user.domain.LoginInfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public void publish(SearchMasterMsg searchMasterMsg, LoginInfo loginInfo) {
        //根据登录用户id获取user对象
        User user=userMapper.lodaUserById(loginInfo.getId());
        //设置user
        searchMasterMsg.setUser(user);
        //根据地址查询经纬度
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        //查询所有商家
        List<Shop> shops = shopMapper.findAll();
        //查询离寻主最近的商家
        Shop shop = DistanceUtil.getNearestShop(point, shops);
        //设置shop
        searchMasterMsg.setShop(shop);
        //保存寻主信息
        super.save(searchMasterMsg);
    }

}
